package com.work.service.impl;

import com.work.bean.Middle;
import com.work.dao.DBDao;
import com.work.dao.impl.DBImpl;
import com.work.service.MiddleService;
import com.work.utils.Constants;
import com.work.utils.SQLUtil;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/10/7.
 */
public class MiddleServiceImpl implements MiddleService {

    DBDao dao;

    public MiddleServiceImpl() {
        dao = new DBImpl();
    }

    @Override
    public boolean insertMiddle(Middle middle) {
        return dao.insertAndDelAndUpdate(SQLUtil.insertMiddleSql(middle));
    }

    @Override
    public String getFinish(int workId) {

        //获取格式化对象
        NumberFormat nt = NumberFormat.getPercentInstance();
        //设置百分数精确度2即保留两位小数
        nt.setMinimumFractionDigits(2);

        int size = findByWorkId(workId).size();
        String format = null;
        if (size > 0) {
            format = nt.format( ((float)findByFinish(1, workId).size() /(float) size));
        }

        return format;
    }

    @Override
    public List<Middle> findByFinish(int finish, int workId) {
        return dao.find(SQLUtil.findByFinishSql(finish, workId), Constants.MIDDLE);
    }

    @Override
    public List<Middle> findByWorkId(int workId) {
        return dao.find(SQLUtil.findByWorkIdSql(workId), Constants.MIDDLE);
    }

//    @Override
//    public List<Middle> findByStudentId(int studentId) {
//        return dao.;
//    }

    @Override
    public HashMap<Integer, List> pageByFinish(int countPage, int whichPage, int isFinish,int workId) {
        return dao.page(countPage,whichPage,SQLUtil.findByFinishNSql(isFinish,workId),Constants.FINISH);
    }

    @Override
    public HashMap<Integer, List> pageByStudentId(int countPage, int whichPage, int isFinish, int studentId) {
        return dao.page(countPage, whichPage, SQLUtil.findByStudentIdSql(isFinish,studentId),Constants.MYWORK);
    }

    @Override
    public boolean updateMiddle(Middle middle) {
        return dao.insertAndDelAndUpdate(SQLUtil.updateMiddle(middle));
    }

    @Override
    public boolean updateGrade(Middle middle) {
        return dao.insertAndDelAndUpdate(SQLUtil.updateGrade(middle));
    }

}
